* Replication
* John Gerring, Carl Henrik Knutsen, Matthew Maguire, Svend-Erik Skaaning, Jan Teorell, and Michael Coppedge. 2021. “Democracy and Human Development: Issues of Conceptualization and Measurement.” Democratization 28(2): 308-332.

* Table C10: Tests of MPI using annual data

* Model 10

* install xtabond2
ssc install xtabond2

* set maxvar
clear
clear matrix
clear mata
set maxvar 20000

* set directory

* load data
use "2021_Democratization_replication_data_posteriors.dta", clear

set matsize 10000

* put democracy index on 0-1 scale
foreach x of varlist mpi_med_stock_10 {
quietly sum `x'
quietly gen `x'_scale = (((`x' - r(min)) * (1 - 0)) / (r(max) - r(min)))
quietly replace `x' = `x'_scale
quietly drop `x'_scale
}
*

*generate 5-year interval dataset
qui drop if year != 2010 & year != 2005 & year != 2000 & year != 1995 & year != 1990 & year != 1985 & year != 1980 & year != 1975 & year != 1970 & year != 1965 & year != 1960 & year != 1955 & year != 1950 & year != 1945 & year != 1940 & year != 1935 & year != 1930 & year != 1925 & year != 1920 & year != 1915 & year != 1910 & year != 1905 & year != 1900 & year != 1895 & year != 1890 & year != 1885 & year != 1880 & year != 1875 & year != 1870 & year != 1865 & year != 1860 & year != 1855 & year != 1850 & year != 1845 & year != 1840 & year != 1835 & year != 1830 & year != 1825 & year != 1820 & year != 1815 & year != 1810 & year != 1805 & year != 1800
qui sort country_id year
qui by country_id: gen trend = _n
qui xtset country_id trend

* Run baseline model
qui xi: xtabond2 f.imr_gapminder_ipo_ln imr_gapminder_ipo_ln mpi_med_stock_10 e_migdppcln_ipo year, gmm(edi_mult_stock_10 e_migdppcln_ipo, lag (3 5)) iv(year) robust artests(3)
qui outreg2 using Table_2_Model_10.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle(" ") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N)) dta replace

***Monte Carlo Estimates Using UDS 3202 Draw Posterior Distribution***
*Run the monte carlo

forvalues i = 2(1)901 {

*Print out an iteration number
display `i'

* put democracy index on 0-1 scale
foreach x of varlist mpi_v`i'_stock_10 {
quietly sum `x'
quietly gen `x'_scale = (((`x' - r(min)) * (1 - 0)) / (r(max) - r(min)))
quietly replace `x' = `x'_scale
quietly drop `x'_scale
}
*


*Fit the model, using the ith draw from the UDS posterior
qui xi: xtabond2 f.imr_gapminder_ipo_ln imr_gapminder_ipo_ln mpi_v`i'_stock_10 e_migdppcln_ipo year, gmm(edi_mult_stock_10 e_migdppcln_ipo, lag (3 5)) iv(year) robust artests(3)
qui outreg2 using Table_2_Model_10.dta, label se bdec(3) noni noobs nor nocon nonot drop(i.year) ctitle("`i'") addstat("Years", e(g_max), "Countries", e(N_g), "Observations", e(N)) dta append

*Extract the coefficients and variance-covariance matrix
matrix b = e(b)
matrix V = e(V)
local blength = colsof(b)

*Preserve the dataset, take a single multivariate normal draw from the
*posterior distribution of the coefficients, and restore the dataset.
*We use the capture command to catch possible errors in drawnorm
*and drop these iterations gracefully.
preserve 
capture quietly drawnorm b1-b`blength', double n(1) means(b) cov(V) clear
if _rc==0 {
mkmat b1-b`blength', matrix(bsample)
matrix posterior = nullmat(posterior) \ bsample
}
else {
display "Error drawing sample...iteration dropped"
}
restore

*Closes the Monte Carlo Loop
}

*Get posterior ready to work with
svmat posterior

*Calculate means and standard deviations
*tabstat posterior*, stat(mean sd)
keep posterior*
quietly outreg2 using Table_2_Model_10, eqkeep(mean sd) bdec(3) replace sum(log) excel
